;; https://projecteuler.net/problem=5

;; Smallest multiple

;; Problem 5

;; 2520 is the smallest number that can be divided by each
;; of the numbers from 1 to 10 without any remainder.

;; What is the smallest positive number that is evenly
;; divisible by all of the numbers from 1 to 20?


(import
  (except (rnrs base) let-values)
  (only (guile) lambda* λ)
  (srfi srfi-1))


(define divisable?
  (λ (num fac)
    (= (remainder num fac) 0)))


(define divisible-by-numbers?
  (λ (num factors)
    (reduce
     (λ (fac acc)
       (and acc (divisable? num fac)))
     #t
     factors)))


(define find-smallest-multiple
  (λ (factors)
    (let iter ([num 1])
      (cond
       [(divisible-by-numbers? num factors) num]
       [else
        (iter (+ num 1))]))))


;; This might take a while to compute.
(simple-format
 (current-output-port)
 "~a\n"
 (find-smallest-multiple
  '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20)))  ; 232792560
